In this part of the exercise, we now need to put the data which we have procured about the funding levels of the different universities that are located in different cantons onto a canton map. We will do so using Folio and take the example TopoJSON mapping which they use.


In [15]:
import folium
import pandas as pd

In [1]:
# Test seeing Switzerland
ch_map = folium.Map(location=[47.3769, 8.5417], tiles='Stamen Toner',
                    zoom_start=13)
ch_map.save('stamen_toner.html')
ch_map


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-987c6044b733> in <module>()
      1 # Test seeing Switzerland
----> 2 ch_map = folium.Map(location=[47.3769, 8.5417], tiles='Stamen Toner',
      3                     zoom_start=13)
      4 ch_map.save('stamen_toner.html')
      5 ch_map

NameError: name 'folium' is not defined

Now do the TopoJSON overlay


In [61]:
# Import the Switzerland map (from the folio pylib notebook)
topo_path = r'ch-cantons.topojson.json'
# Import our csv file with all of the values for the amounts of the grants 
data = 'P3_GrantExport.csv'

# Insert coordinates that are for Switzerland (i.e. 9.594226,47.525058)
ch_map = folium.Map(location=[46.8769, 8.6017], tiles='Mapbox Bright',
                    zoom_start=7)
ch_map.choropleth(geo_path=topo_path, topojson='objects.ch-cantons')
ch_map.save('ch_map.html')
ch_map


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-61-5e82e4b8b023> in <module>()
      8                     zoom_start=7)
      9 ch_map.choropleth(geo_path=topo_path, topojson='objects.ch-cantons')
---> 10 ch_map.save('ch_map.html')
     11 ch_map

/Users/paramoed/anaconda/lib/python3.5/site-packages/folium/element.py in save(self, outfile, close_file, **kwargs)
    151 
    152         root = self.get_root()
--> 153         html = root.render(**kwargs)
    154         fid.write(html.encode('utf8'))
    155         if close_file:

/Users/paramoed/anaconda/lib/python3.5/site-packages/folium/element.py in render(self, **kwargs)
    357         """Renders the HTML representation of the element."""
    358         for name, child in self._children.items():
--> 359             child.render(**kwargs)
    360         return self._template.render(this=self, kwargs=kwargs)
    361 

/Users/paramoed/anaconda/lib/python3.5/site-packages/folium/element.py in render(self, **kwargs)
    665 
    666         for name, element in self._children.items():
--> 667             element.render(**kwargs)

/Users/paramoed/anaconda/lib/python3.5/site-packages/folium/features.py in render(self, **kwargs)
    478     def render(self, **kwargs):
    479         """Renders the HTML representation of the element."""
--> 480         super(TopoJson, self).render(**kwargs)
    481 
    482         figure = self.get_root()

/Users/paramoed/anaconda/lib/python3.5/site-packages/folium/element.py in render(self, **kwargs)
    661         script = self._template.module.__dict__.get('script', None)
    662         if script is not None:
--> 663             figure.script.add_children(Element(script(self, kwargs)),
    664                                        name=self.get_name())
    665 

/Users/paramoed/anaconda/lib/python3.5/site-packages/jinja2/runtime.py in __call__(self, *args, **kwargs)
    434             raise TypeError('macro %r takes not more than %d argument(s)' %
    435                             (self.name, len(self.arguments)))
--> 436         return self._func(*arguments)
    437 
    438     def __repr__(self):

<template> in macro(l_this, l_kwargs)

/Users/paramoed/anaconda/lib/python3.5/site-packages/jinja2/runtime.py in call(_Context__self, _Context__obj, *args, **kwargs)
    194                 args = (__self.environment,) + args
    195         try:
--> 196             return __obj(*args, **kwargs)
    197         except StopIteration:
    198             return __self.environment.undefined('value was undefined because '

/Users/paramoed/anaconda/lib/python3.5/site-packages/folium/features.py in style_data(self)
    471             else:
    472                 return data
--> 473         geometries = recursive_get(self.data, self.object_path.split('.'))['geometries']  # noqa
    474         for feature in geometries:
    475             feature.setdefault('properties', {}).setdefault('style', {}).update(self.style_function(feature))  # noqa

TypeError: 'NoneType' object is not subscriptable

In [ ]:
# Need to use colors wisely - becaue this is continuous and not descrete, we will be using different shades of green

In [ ]:
#Catarina's test
import folium
import pandas as pd

topo_path = r'ch-cantons.topojson.json'

grants_data = pd.read_csv(state_unemployment)

#Let Folium determine the scale
map = folium.Map(location=[47.3769, 8.5417], zoom_start=13)
map.choropleth(geo_path=state_geo, data=grants_data,
             columns=['Canton Shortname', 'Total Sum'],
             key_on='feature.id',
             fill_color='YlGn', fill_opacity=0.7, line_opacity=0.2,
             legend_name='Total Grants Received (CHF)')
map.save('swiss_grants.html')